package cn.com.headfree.workflow.workflowapprove.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gta.edu.sdk.base.service.impl.BaseServiceImpl;
import com.gta.edu.sdk.common.dto.Page;
import com.gta.edu.sdk.common.dto.ResultData;
import com.gta.edu.sdk.util.StringUtil;

import cn.com.headfree.call.IFrontFrameServiceCall;
import cn.com.headfree.workflow.dto.ApproveWorkFlowDto;
import cn.com.headfree.workflow.dto.PersonData;
import cn.com.headfree.workflow.dto.WorkFlowConditionJSONData;
import cn.com.headfree.workflow.dto.WorkFlowDto;
import cn.com.headfree.workflow.dto.WorkFlowJSONObject;
import cn.com.headfree.workflow.dto.WorkFlowLinesJSONData;
import cn.com.headfree.workflow.dto.WorkFlowNodeStatus;
import cn.com.headfree.workflow.util.ParseWorkFlowDataUtil;
import cn.com.headfree.workflow.workflowapprove.dao.IWorkFlowApproveDao;
import cn.com.headfree.workflow.workflowapprove.model.TWorkFlowApprove;
import cn.com.headfree.workflow.workflowapprove.model.TWorkFlowNodeApprove;
import cn.com.headfree.workflow.workflowapprove.service.IWorkFlowApproveService;
import cn.com.headfree.workflow.workflowinfo.model.TWorkFlowInfo;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Service("workFlowApproveService")
public class WorkFlowApproveServiceImpl extends BaseServiceImpl<TWorkFlowApprove> implements IWorkFlowApproveService{
	private static final Logger log = Logger.getLogger(WorkFlowApproveServiceImpl.class);
	@Autowired
	private IWorkFlowApproveDao workFlowApproveDao;
	@Autowired
	private IFrontFrameServiceCall frontFrameServiceCall;
	private static final String USER_TYPE = "user";
	private static final String ROLE_TYPE = "dept";
	private static final String AGGREGATION = "aggregation";// 聚合
	private static final String END = "end";
	private static final String START = "start";

	public boolean saveApproveRecord(TWorkFlowApprove approve, String currentUserId, String approveAdvise,String domainId) throws SQLException {
//		if (saveObj != null) {
//			this.save(saveObj);
//			approve.setBusinessId(saveObj.getId());
//		}
		TWorkFlowApprove approveRecord = null;
		if (approve.getId() != null) {
			approveRecord = this.get(TWorkFlowApprove.class, approve.getId());
		} else {
			approveRecord = approve;
		}
		// 获得当前的工作流对象
		TWorkFlowInfo wfi = this.get(TWorkFlowInfo.class, approveRecord.getWorkFlowId());
		if (wfi != null) {
			// 获得当前业务工作流中所有的审批记录
			List<TWorkFlowApprove> flaList = this.workFlowApproveDao.getApproveFlow(approveRecord.getWorkFlowId(), approveRecord.getBusinessId());
			if (flaList.isEmpty()) {
				// 当前工作流未进行过审批时
				this.saveFirstApproveRecord(wfi, approveRecord.getBusinessId(), approveRecord.getWorkFlowId(),domainId);
			} else {
				WorkFlowDto dto = ParseWorkFlowDataUtil.getInstance().getCurrentNode(wfi.getId(), wfi.getWorkFlowCode(),approveRecord.getNodeId(), approve.getApproveCondition());
				// 保存当前审批人的审批结果
				TWorkFlowNodeApprove wfna = this.workFlowApproveDao.getWorkFlowNodeApprove(approveRecord.getId(), currentUserId);
				wfna.setApproveAdvise(approveAdvise);
				wfna.setApproveCondition(approve.getApproveCondition());
				wfna.setApproveResult(approve.getApproveResult());
				wfna.setApproveStatus(1);
				wfna.setApproveTime(new Date());
				this.saveOrUpdate(wfna);
				List<TWorkFlowNodeApprove> wfnaList = this.workFlowApproveDao.getWorkFlowNodeApproveByApproveId(approve.getId());
				if (!StringUtil.isNullOrEmpty(dto.getSelMethod()) && AGGREGATION.equals(dto.getSelMethod())) {
					// 修改所有的人员审批记录不再是待审批状态
					if (wfnaList != null && wfnaList.size() > 0) {
						TWorkFlowNodeApprove temp = null;
						for (int i=0;i<wfnaList.size();i++) {
							temp = wfnaList.get(i);
							if (!currentUserId.equals(temp.getApprover())) {
								temp.setApproveStatus(1);
								this.saveOrUpdate(temp);
							}
						}
					}
				}
				boolean result = true;
				if (wfna.getApproveResult().equals(2)) {
					// 当前审批不通过，设定流程结束
					approveRecord.setApproveResult(2);
				} else {
					if (this.isNodeApproveEnd(dto, wfnaList)) {
						approveRecord.setApproveResult(1);
						approveRecord.setApproveCondition(wfna.getApproveCondition());
						approveRecord.setNextNodeId(dto.getNextNodeId());
						if (!dto.getNextNodeId().equals(END)) {
							// 写入下一个审批的记录
							this.writeNextFlowNode(dto.getNextNode(), approveRecord,domainId);
							result = false;
						}
					}
				}
				this.saveOrUpdate(approveRecord);
				return result;
			}
		}
		return false;
	}

	/**
	 * @description 写下个流程节点的审批记录数据
	 * @author fengya
	 * @date 2015-9-11 上午11:41:46
	 * @param dto
	 * @param approveRecord
	 * @return void
	 */
	private void writeNextFlowNode(WorkFlowDto dto, TWorkFlowApprove approve,String domainId) {
		TWorkFlowApprove nextApproveRecord = new TWorkFlowApprove();
		nextApproveRecord.setArriveTime(new Date());
		nextApproveRecord.setBusinessId(approve.getBusinessId());
		nextApproveRecord.setNodeId(dto.getNodeId());
		nextApproveRecord.setNodeName(dto.getNodeName());
		nextApproveRecord.setWorkFlowId(approve.getWorkFlowId());
		this.save(nextApproveRecord);
		List<PersonData> approvers = dto.getApprovers();
		if (approvers != null) {
			TWorkFlowNodeApprove wfna = null;
			Iterator<PersonData> pdIt = approvers.iterator();
			PersonData pd = null;
			List<TWorkFlowNodeApprove> wfnaList = null;
			while (pdIt.hasNext()) {
				pd = pdIt.next();
				if (pd.getType().equals(USER_TYPE)) {
					wfna = new TWorkFlowNodeApprove();
					wfna.setApprover(pd.getId());
					wfna.setApproveResult(0);
					wfna.setApproverName(pd.getName());
					wfna.setApproveId(nextApproveRecord.getId());
					this.save(wfna);
				} else if (pd.getType().equals(ROLE_TYPE)) {
					wfnaList = this.getWorkFlowApproveByDept(pd.getId(), nextApproveRecord.getId(),domainId);
					for(TWorkFlowNodeApprove app : wfnaList) {
						this.saveOrUpdate(app);
					}
				}
			}
		}
	}

	/**
	 * @description 判断该节点的审批是否已经结束
	 * @author fengya
	 * @date 2015-9-28 下午03:58:58
	 * @param dto
	 * @param wfnaList
	 * @return
	 * @return boolean
	 */
	private boolean isNodeApproveEnd(WorkFlowDto dto, List<TWorkFlowNodeApprove> wfnaList) {
		if (dto.getSelMethod().equals(AGGREGATION)) {
			// 如果是聚合审批情况下，该节点审批结束
			// 如果是聚合，修改其他人为审批后的结果
			return true;
		} else {
			// 如果是组合审批情况下，判断是否所有的审批人员都已经审批
			return this.nodeApproveIsFinish(wfnaList);
		}
	}

	/**
	 * @description 判断某个流程节点在组合情况下是否全部审批通过
	 * @author fengya
	 * @date 2015-9-11 上午11:33:36
	 * @param nodeId
	 * @param flaList
	 * @return
	 * @return boolean
	 */
	private boolean nodeApproveIsFinish(List<TWorkFlowNodeApprove> wfnaList) {
		Iterator<TWorkFlowNodeApprove> wfaIt = wfnaList.iterator();
		TWorkFlowNodeApprove wfa = null;
		while (wfaIt.hasNext()) {
			wfa = wfaIt.next();
			if (wfa.getApproveTime() == null) {
				return false;
			}
		}
		return true;
	}

	/**
	 * @description 对审批记录进行按照流程节点分组
	 * @author fengya
	 * @date 2015-9-11 上午11:18:26
	 * @param flaList
	 * @return
	 * @return Map<String,List<WorkFlowApprove>>
	 */
	private Map<String, List<TWorkFlowApprove>> getWorkFlowApproveByNodeGroup(List<TWorkFlowApprove> flaList) {
		Map<String, List<TWorkFlowApprove>> wfaMap = new HashMap<String, List<TWorkFlowApprove>>();
		TWorkFlowApprove wfa = null;
		Iterator<TWorkFlowApprove> wfaIt = flaList.iterator();
		while (wfaIt.hasNext()) {
			wfa = wfaIt.next();
			if (!wfaMap.containsKey(wfa.getNodeId())) {
				wfaMap.put(wfa.getNodeId(), new ArrayList<TWorkFlowApprove>());
			}
			wfaMap.get(wfa.getNodeId()).add(wfa);
		}
		return wfaMap;
	}

	/**
	 * @description 对节点的审批记录进行分组
	 * @author fengya
	 * @date 2015-9-25 下午04:47:04
	 * @param wfaMap
	 * @param wfnaList
	 * @return
	 * @return Map<WorkFlowApprove,List<WorkFlowNodeApprove>>
	 */
	private Map<TWorkFlowApprove, List<TWorkFlowNodeApprove>> getWorkFlowNodeApproveByAppGroup(Map<String, List<TWorkFlowApprove>> wfaMap, List<TWorkFlowNodeApprove> wfnaList) {
		Map<TWorkFlowApprove, List<TWorkFlowNodeApprove>> approveRecords = new HashMap<TWorkFlowApprove, List<TWorkFlowNodeApprove>>();
		Iterator<Entry<String, List<TWorkFlowApprove>>> wfaIt = wfaMap.entrySet().iterator();
		Entry<String, List<TWorkFlowApprove>> entry = null;
		Map<String, List<TWorkFlowNodeApprove>> wfnaMap = this.getWorkFlowNodeApproveByApproveId(wfnaList);
		TWorkFlowApprove wfa = null;
		while (wfaIt.hasNext()) {
			entry = wfaIt.next();
			wfa = this.getLastApproveRecord(entry.getValue());

			approveRecords.put(wfa, this.getNodeApproveRecord(entry.getValue(), wfnaMap));
		}
		return approveRecords;
	}

	/**
	 * @description 获得节点的所有审批记录
	 * @author fengya
	 * @date 2015-9-29 上午11:21:28
	 * @param wfaList
	 * @param wfnaMap
	 * @return
	 * @return List<WorkFlowNodeApprove>
	 */
	private List<TWorkFlowNodeApprove> getNodeApproveRecord(List<TWorkFlowApprove> wfaList, Map<String, List<TWorkFlowNodeApprove>> wfnaMap) {
		List<TWorkFlowNodeApprove> tempList = new ArrayList<TWorkFlowNodeApprove>();
		Iterator<TWorkFlowApprove> wfaIt = wfaList.iterator();
		TWorkFlowApprove wfa = null;
		List<TWorkFlowNodeApprove> wfnaList = null;
		Iterator<TWorkFlowNodeApprove> wfnaIt = null;
		TWorkFlowNodeApprove wfna = null;
		while (wfaIt.hasNext()) {
			wfa = wfaIt.next();
			wfnaList = wfnaMap.get(wfa.getId());
			wfnaIt = wfnaList.iterator();
			while (wfnaIt.hasNext()) {
				wfna = wfnaIt.next();
				wfna.setArriveTime(wfa.getArriveTime());
				wfna.setApproveLimit(wfa.getApproveLimit());
			}
			tempList.addAll(wfnaMap.get(wfa.getId()));
		}
		return tempList;
	}

	/**
	 * @description 对审批记录进行根据节点中的审批Id进行分组
	 * @author fengya
	 * @date 2015-9-25 下午04:44:02
	 * @param wfnaList
	 * @return
	 * @return Map<String,List<WorkFlowNodeApprove>>
	 */
	private Map<String, List<TWorkFlowNodeApprove>> getWorkFlowNodeApproveByApproveId(List<TWorkFlowNodeApprove> wfnaList) {
		Map<String, List<TWorkFlowNodeApprove>> wfnaMap = new HashMap<String, List<TWorkFlowNodeApprove>>();
		Iterator<TWorkFlowNodeApprove> wfnaIt = wfnaList.iterator();
		TWorkFlowNodeApprove wfna = null;
		while (wfnaIt.hasNext()) {
			wfna = wfnaIt.next();
			if (!wfnaMap.containsKey(wfna.getApproveId())) {
				wfnaMap.put(wfna.getApproveId(), new ArrayList<TWorkFlowNodeApprove>());
			}
			wfnaMap.get(wfna.getApproveId()).add(wfna);
		}
		return wfnaMap;
	}

	/**
	 * @description 当业务数据进行第一次保存时，启动流程的第一个审批人员的添加
	 * @author fengya
	 * @date 2015-9-9 下午04:54:44
	 * @param wfi
	 * @param businessId
	 * @param workFlowId
	 * @throws SQLException
	 * @return void
	 */
	private void saveFirstApproveRecord(TWorkFlowInfo wfi, String businessId, String workFlowId,String domainId) throws SQLException {
		WorkFlowDto dto = ParseWorkFlowDataUtil.getInstance().getFirstApprover(wfi.getId(), wfi.getWorkFlowCode());
		TWorkFlowApprove approve = new TWorkFlowApprove();
		approve.setBusinessId(businessId);
		approve.setWorkFlowId(workFlowId);
		this.writePreApproveRecord(dto, approve,domainId);
	}

	/**
	 * @description 写预审批的记录数据
	 * @author fengya
	 * @date 2015-9-11 上午11:48:18
	 * @param dto
	 * @param approve
	 * @return void
	 */
	private void writePreApproveRecord(WorkFlowDto dto, TWorkFlowApprove approve,String domainId) {
		List<PersonData> approvers = dto.getApprovers();
		if (approvers != null) {
			TWorkFlowApprove wfa = null;
			wfa = new TWorkFlowApprove();
			wfa.setArriveTime(new Date());
			wfa.setBusinessId(approve.getBusinessId());
			wfa.setWorkFlowId(approve.getWorkFlowId());
			wfa.setNodeId(dto.getNodeId());
			wfa.setApproveLimit(dto.getApproveLimit());
			wfa.setNodeName(dto.getNodeName());
			this.save(wfa);
			Iterator<PersonData> pdIt = approvers.iterator();
			PersonData pd = null;
			TWorkFlowNodeApprove wfna = null;
			List<TWorkFlowNodeApprove> wfnaList = null;
			while (pdIt.hasNext()) {
				pd = pdIt.next();
				if (pd.getType().equals(USER_TYPE)) {
					wfna = new TWorkFlowNodeApprove();
					wfna.setApprover(pd.getId());
					wfna.setApproverName(pd.getName());
					wfna.setApproveId(wfa.getId());
					wfna.setApproveResult(0);
					wfna.setArriveTime(new Date());
					this.save(wfna);
				} else if (pd.getType().equals(ROLE_TYPE)) {
					wfnaList = this.getWorkFlowApproveByDept(pd.getId(), wfa.getId(),domainId);
					for(TWorkFlowNodeApprove app : wfnaList) {
						this.saveOrUpdate(app);
					}
				}
			}
		}
	}

	/**
	 * @description 根据机构ID生成该机构下所有的人员审批记录
	 * @author fengya
	 * @date 2015-9-9 下午04:49:44
	 * @param deptId
	 * @param businessId
	 * @param workFlowId
	 * @param dto
	 * @return
	 * @return List<WorkFlowApprove>
	 */
	private List<TWorkFlowNodeApprove> getWorkFlowApproveByDept(String deptId, String approveId,String domainId) {
		List<TWorkFlowNodeApprove> wfaList = new ArrayList<TWorkFlowNodeApprove>();
		ResultData userStr = this.frontFrameServiceCall.findUserByLabelId(deptId,domainId);
		log.info("userStr=" + userStr);
		if (userStr.isSuccess()) {
			TWorkFlowNodeApprove wfna = null;
			JSONArray array = JSONArray.fromObject(userStr.getData());
			if (array != null) {
				JSONObject obj = null;
				for (int i=0;i<array.size();i++) {
					obj = array.getJSONObject(i);
					wfna = new TWorkFlowNodeApprove();
					wfna.setApprover(obj.getString("id"));
					wfna.setApproverName(obj.getString("loginName"));
					wfna.setApproveId(approveId);
					wfna.setApproveResult(0);
					wfaList.add(wfna);
				}
			}
		} else {
			log.info(userStr.getErrorMsg());
		}
		return wfaList;
	}

	public List<WorkFlowConditionJSONData> getNextWorkFlowCondition(String approveId) throws SQLException {
		TWorkFlowApprove approve = this.get(TWorkFlowApprove.class, approveId);
		if (approve != null) {
			TWorkFlowInfo info = this.get(TWorkFlowInfo.class, approve.getWorkFlowId());
			List<WorkFlowConditionJSONData> list = ParseWorkFlowDataUtil.getInstance().getNextNodeCondition(info.getId(),info.getWorkFlowCode(), approve.getNodeId());
			if (!list.isEmpty()) {
				return list;
			}
		}
		return null;
	}

	public WorkFlowNodeStatus findWorkFlowNodes(String workFlowId, String businessId, String code) {
		WorkFlowNodeStatus workFlowNodeStatus = new WorkFlowNodeStatus();
		List<TWorkFlowApprove> approvedFlowList = this.workFlowApproveDao.getApproveFlow(workFlowId, businessId);
		List<TWorkFlowNodeApprove> wfnaList = this.workFlowApproveDao.getWorkFlowNodeApproveByBusinessId(businessId);
		Map<String, List<TWorkFlowApprove>> wfaMap = this.getWorkFlowApproveByNodeGroup(approvedFlowList);
		Map<TWorkFlowApprove, List<TWorkFlowNodeApprove>> wfnaMap = this.getWorkFlowNodeApproveByAppGroup(wfaMap, wfnaList);
		Iterator<Entry<TWorkFlowApprove, List<TWorkFlowNodeApprove>>> wfaIt = wfnaMap.entrySet().iterator();
		Entry<TWorkFlowApprove, List<TWorkFlowNodeApprove>> entry = null;
		TWorkFlowApprove wfa = null;
		while (wfaIt.hasNext()) {
			entry = wfaIt.next();
			wfa = entry.getKey();
			if (!wfa.getApproveResult().equals(0)) {
				if (wfa.getApproveResult().equals(1)) {
					if (workFlowNodeStatus.getApprovedNodes().containsKey(wfa.getNodeId())) {
						workFlowNodeStatus.getApprovedNodes().get(wfa.getNodeId()).addAll(entry.getValue());
					} else {
						workFlowNodeStatus.getApprovedNodes().put(wfa.getNodeId(), entry.getValue());
					}
				} else {
					if (workFlowNodeStatus.getApproveFailures().containsKey(wfa.getNodeId())) {
						workFlowNodeStatus.getApproveFailures().get(wfa.getNodeId()).addAll(entry.getValue());
					} else {
						workFlowNodeStatus.getApproveFailures().put(wfa.getNodeId(), entry.getValue());
					}
				}
			} else {
				if (workFlowNodeStatus.getWaitApproveNodes().containsKey(wfa.getNodeId())) {
					workFlowNodeStatus.getWaitApproveNodes().get(wfa.getNodeId()).addAll(entry.getValue());
				} else {
					workFlowNodeStatus.getWaitApproveNodes().put(wfa.getNodeId(), entry.getValue());
				}
			}
		}
		WorkFlowJSONObject workFlowJSONObject = ParseWorkFlowDataUtil.getInstance().getWorkFlowJSONObject(code);
		this.getConnLineLoad(wfaMap, workFlowJSONObject, START);
		workFlowNodeStatus.setWorkFlowJSONObject(workFlowJSONObject);
		return workFlowNodeStatus;
	}

	/**
	 * @description 得到审批记录节点中的最后一次审批的记录数据
	 * @author fengya
	 * @date 2015-9-25 上午11:35:49
	 * @param wfaList
	 * @return
	 * @return WorkFlowApprove
	 */
	private TWorkFlowApprove getLastApproveRecord(List<TWorkFlowApprove> wfaList) {
		Iterator<TWorkFlowApprove> wfaIt = wfaList.iterator();
		TWorkFlowApprove wfa = null;
		TWorkFlowApprove maxApp = null;
		while (wfaIt.hasNext()) {
			wfa = wfaIt.next();
			if (maxApp != null) {
				if (wfa.getArriveTime().after(maxApp.getArriveTime())) {
					maxApp = wfa;
				}
			} else {
				maxApp = wfa;
			}
		}
		return maxApp;
	}

	/**
	 * @description 获得节点所有经过的路径
	 * @author fengya
	 * @date 2015-9-28 下午01:56:25
	 * @param nodeIds
	 * @param lineJsons
	 * @param startNodeId
	 * @return void
	 */
	private void getConnLineLoad(Map<String, List<TWorkFlowApprove>> wfaMap, WorkFlowJSONObject workFlowJSONObject, String startNodeId) {
		Map<String, WorkFlowLinesJSONData> lines = workFlowJSONObject.getLines();
		Collection<WorkFlowLinesJSONData> lineJsons = lines.values();
		List<WorkFlowLinesJSONData> connLines = this.getWorkFlowLinesJSONDataByNode(lineJsons, startNodeId);
		WorkFlowLinesJSONData line = null;
		// 判断该节点连接的线路是否已经遍历过
		Iterator<WorkFlowLinesJSONData> lineIt = connLines.iterator();
		while (lineIt.hasNext()) {
			if(lineIt.next().getMarked()){
				return;
			}
		}
		if (startNodeId.equals(START)) {
			// 第一个是开始节点时
			lineIt = connLines.iterator();
			while (lineIt.hasNext()) {
				line = lineIt.next();
				if (wfaMap.containsKey(line.getTo())) {
					line.setMarked(true);
					if(!line.getTo().equals(END)){
						this.getConnLineLoad(wfaMap, workFlowJSONObject, line.getTo());
					}
				}
			}
		} else {
			// 不是第一个开始节点时
			List<TWorkFlowApprove> wfaList = wfaMap.get(startNodeId);
			Iterator<TWorkFlowApprove> wfaIt = wfaList.iterator();
			TWorkFlowApprove wfa = null;
			while (wfaIt.hasNext()) {
				wfa = wfaIt.next();
				if (StringUtil.isNullOrEmpty(wfa.getNextNodeId())) {
					continue;
				} else {
					this.connectNodeLine(lineJsons, wfa);
					if(!wfa.getNextNodeId().equals(END)){
						this.getConnLineLoad(wfaMap, workFlowJSONObject, wfa.getNextNodeId());
					}
				}
			}
		}
	}

	private void connectNodeLine(Collection<WorkFlowLinesJSONData> connLines, TWorkFlowApprove wfa) {
		Iterator<WorkFlowLinesJSONData> lineIt = connLines.iterator();
		WorkFlowLinesJSONData line = null;
		if (StringUtil.isNullOrEmpty(wfa.getApproveCondition())) {
			while (lineIt.hasNext()) {
				line = lineIt.next();
				if (line.getFrom().equals(wfa.getNodeId()) && line.getTo().equals(wfa.getNextNodeId())) {
					line.setMarked(true);
					break;
				}
			}
		} else {
			// 当带有条件的节点处理
			// 获得该节点连接到条件节点的线路
			List<WorkFlowLinesJSONData> lines = this.getLineByStartNode(connLines, wfa.getNodeId());
			List<WorkFlowLinesJSONData> nextLines = null;
			if (!lines.isEmpty()) {
				lineIt = lines.iterator();
				WorkFlowLinesJSONData tempLine = null;
				Iterator<WorkFlowLinesJSONData> nextListIt = null;
				while (lineIt.hasNext()) {
					line = lineIt.next();
					line.setMarked(true);
					// 获得条件节点下连接的所有的其他节点线路
					nextLines = this.getLineByStartNode(connLines, line.getTo());
					nextListIt = nextLines.iterator();
					while (nextListIt.hasNext()) {
						tempLine = nextListIt.next();
						if (tempLine.getTo().equals(wfa.getNextNodeId())) {
							tempLine.setMarked(true);
							break;
						}
					}
				}
			}
		}
	}

	private List<WorkFlowLinesJSONData> getLineByStartNode(Collection<WorkFlowLinesJSONData> connLines, String startNodeId) {
		List<WorkFlowLinesJSONData> temp = new ArrayList<WorkFlowLinesJSONData>();
		Iterator<WorkFlowLinesJSONData> lineIt = connLines.iterator();
		WorkFlowLinesJSONData line = null;
		while (lineIt.hasNext()) {
			line = lineIt.next();
			if (line.getFrom().equals(startNodeId)) {
				temp.add(line);
			}
		}
		return temp;
	}

	/**
	 * @description 根据起始节点的名称查找从起始节点对应的线
	 * @author fengya
	 * @date 2015-9-28 下午01:25:25
	 * @param lineJsons
	 * @param startNodeId
	 * @return
	 * @return List<WorkFlowLinesJSONData>
	 */
	private List<WorkFlowLinesJSONData> getWorkFlowLinesJSONDataByNode(Collection<WorkFlowLinesJSONData> lineJsons, String startNodeId) {
		Iterator<WorkFlowLinesJSONData> lineIt = lineJsons.iterator();
		WorkFlowLinesJSONData line = null;
		List<WorkFlowLinesJSONData> lineList = new ArrayList<WorkFlowLinesJSONData>();
		while (lineIt.hasNext()) {
			line = lineIt.next();
			if (line.getFrom().equals(startNodeId)) {
				lineList.add(line);
			}
		}
		return lineList;
	}

	@Override
	public void findWaitApproveWorkFlow(Page<ApproveWorkFlowDto> page, String userId, String sysCode) {
		this.workFlowApproveDao.findWaitApproveWorkFlow(page, userId, sysCode);
	}

	@Override
	public void findApprovedWorkFlow(Page<ApproveWorkFlowDto> page, String userId, String sysCode) {
		this.workFlowApproveDao.findApprovedWorkFlow(page, userId, sysCode);
	}

	@Override
	public List<TWorkFlowNodeApprove> findApprovedRecord(String businessId) {
		return this.workFlowApproveDao.getWorkFlowNodeApproveByBusinessId(businessId);
	}

}
